package top.iflyoung.cim.common.util;

import top.iflyoung.cim.common.exception.CIMException;
import top.iflyoung.cim.common.pojo.RouteInfo;
import top.iflyoung.cim.common.enums.StatusEnum;

/**
 * Function:
 *
 * @author crossoverJie
 * Date: 2020-04-12 20:42
 * @since JDK 1.8
 */
public class RouteInfoParseUtil {

    public static RouteInfo parse(String info) {
        try {
            String[] serverInfo = info.split(":");
            RouteInfo routeInfo = new RouteInfo(serverInfo[0], Integer.parseInt(serverInfo[1]), Integer.parseInt(serverInfo[2]));
            return routeInfo;
        } catch (Exception e) {
            throw new CIMException(StatusEnum.VALIDATION_FAIL);
        }
    }

    public static String parse(RouteInfo info) {
        if (null == info) {
            throw new NullPointerException("传入路由参数是空值");
        }
        StringBuilder sb = new StringBuilder();
        sb.append(info.getIp());
        sb.append(":").append(info.getCimServerPort());
        sb.append(":").append(info.getHttpPort());
        return sb.toString();
    }
}
